package base.网络编程.UDP;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class 收 {
    public static void main(String[] args) throws IOException {
        DatagramSocket ds = new DatagramSocket(10086);
        byte[] bytes = new byte[1024];
        DatagramPacket dp = new DatagramPacket(bytes,bytes.length);

        //解析数据包
        ds.receive(dp);

        byte[] data = dp.getData();
        int len = dp.getLength();
        InetAddress ip = dp.getAddress();
        String host = ip.getHostName();
        int port = dp.getPort();

        System.out.println("接收到来自"+ip+"的消息："+new String(data,0,len));
        System.out.println("对方端口号为："+port+",总共"+len+"个字节");
    }
}
